- Write a complete C++ program that does the following:
- Prompts the user to enter their name and store their name in a string variable.
- Passes the string to a function called printReverseString that prints the string in reverse (prints from inside the function).
- This function should not change the original string.
- Note that you can use the string method .size() or .length in a for loop when reversing the string.
- Given string s, s.size() or s.length will return the number of characters in string s.
- Print the string inside main to demonstrate that it remains unchanged after passing to printReverseString.
- Passes the string to a function called reverseString that does change the original string passed from main.
- The main function then prints the string to demonstrate that it is now permanently reversed.
Example main function that uses these functions:
int main() {
string name;
cout << "Enter your name: ";
cin >> name;
cout << "String in reverse:\n";
printReverseString(name);
cout << "Current value of name:\n";
cout << name << endl;
reverseString(name);
cout << "Current value of name:\n";
cout << name << endl;
return 0;
}
Sample run of program:
Enter your name: Danny
String in reverse:
ynnaD
Current value of name:
Danny
Current value of name:
ynnaD
- Write a complete C++ program that does the following:
- Prompts the user to enter a string.
- Passes the string to a function called lowerCaseString that converts the string to all lower case. The change in the string should be permanent in main.
- The main function then prints the string.
- Passes the string to a function called upperCaseString that converts the string to all upper case. The change in the string should be permanent in main.
- The main function then prints the string.
- Passes the string to a function called undulatingString that converts the string to mixed case as follows:
- The first letter is lower case, the second letter is upper case, the third letter is lower case, the fourth letter is upper case, and so on, for all letters in the string.
- The change in the string should be permanent in main.
- Hint: Review this code and observe that adding 32 to the starting integer value (when cast as a char) converts an upper case letter to lower case. For further detail, see the ASCII table and note decimal values 65-90 are upper case letters and 97-122 are lower case letters.
Example main function that uses these functions:
int main() {
string s;
cout << "Enter a word: ";
cin >> s;
lowerCaseString(s);
cout << "Fully lower case:\n";
cout << s << endl;
upperCaseString(s);
cout << "Fully upper case:\n";
cout << s << endl;
undulatingString(s);
cout << "Undulating case:\n";
cout << s << endl;
return 0;
}
Sample run of program:
Enter a word: Mathematics
Fully lower case:
mathematics
Fully upper case:
MATHEMATICS
Undulating case:
mAtHeMaTiCs
- Write a complete C++ program that does the following:
- Prompts the user for two lower case letters. The first letter must precede the second in alphabetical order. Also, the characters cannot be the same. For example, 'e' followed' by 'h' are valid inputs, but 'h' followed by 'e' is invalid.
- If the user enters invalid inputs, force the user to enter valid inputs.
- Hint: cast each char as an int and store the result for each in an int variable. Compare the two ints to determine if one precedes the other.
- Pass these two characters as input to a function called printStripedSquare that prints a 5 x 5 square with the following properties:
- Odd numbered rows consist of the first char.
- Even numbered rows consist of the second char.
Example main function that uses printStripedSquare:
int main() {
char a, b;
int x, y;
cout << "Enter two characters, first must precede second in alphabetical order: ";
cin >> a >> b;
x = (int) a;
y = (int) b;
while(FILL IN){
FILL IN
}
printStripedSquare(a, b);
return 0;
}
Sample run of program:
Enter two characters, first must precede second in alphabetical order: t r
Invalid input! First character must precede second in alphabetical order! r t
rrrrr
ttttt
rrrrr
ttttt
rrrrr